package com.xmx.productcenter.service.impl;

import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.xmx.productcenter.entity.GoodsEntity;
import com.xmx.productcenter.service.GoodsService;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.xmx.common.utils.PageUtils;
import com.xmx.common.utils.Query;

import com.xmx.productcenter.dao.CategoryDao;
import com.xmx.productcenter.entity.CategoryEntity;
import com.xmx.productcenter.service.CategoryService;

import javax.annotation.Resource;


@Service("categoryService")
public class CategoryServiceImpl extends ServiceImpl<CategoryDao, CategoryEntity> implements CategoryService {

    @Resource
    private CategoryDao categoryDao;

    @Resource
    private GoodsService goodsService;

    @Override
    public PageUtils queryPage(Map<String, Object> params) {
        IPage<CategoryEntity> page = this.page(
                new Query<CategoryEntity>().getPage(params),
                new QueryWrapper<CategoryEntity>()
        );

        return new PageUtils(page);
    }

    public List<CategoryEntity> queryByPid(Integer pid) {
        return categoryDao.selectList(Wrappers.<CategoryEntity>lambdaQuery().eq(CategoryEntity::getPid, pid).eq(CategoryEntity::getDeleted, false));
    }

    public List<Map> getCategory2GoodsList() {
        List<Map> categoryList = new ArrayList<>();
        //10 ：这里可以通过系统配置 首页显示显示的分类数量
        List<CategoryEntity> catL1List = queryL1WithoutRecommend(0, 4);
        for (CategoryEntity catL1 : catL1List) {
            List<CategoryEntity> catL2List = queryByPid(catL1.getId());
            List<Integer> l2List = new ArrayList<>();
            for (CategoryEntity catL2 : catL2List) {
                l2List.add(catL2.getId());
            }

            List<GoodsEntity> categoryGoods;
            if (l2List.size() == 0) {
                categoryGoods = new ArrayList<>();
            } else {
                synchronized (this) {
                    categoryGoods = goodsService.queryByCategory(l2List, 0, 4);
                }
            }

            Map<String, Object> catGoods = new HashMap<>();
            catGoods.put("id", catL1.getId());
            catGoods.put("name", catL1.getName());
            catGoods.put("goodsList", categoryGoods);
            categoryList.add(catGoods);
        }
        return categoryList;
    }

    public List<CategoryEntity> queryL1WithoutRecommend(int offset, int limit) {
        Page page = categoryDao.selectPage(new Page(offset, limit), Wrappers.<CategoryEntity>lambdaQuery().
                eq(CategoryEntity::getLevel, "L1").
                ne(CategoryEntity::getName, "推荐").
                eq(CategoryEntity::getDeleted, false));
        return page.getRecords();

    }



}